package com.lyh.yobeiapplication.helper;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.lyh.yobeiapplication.R;

public class DialogHelper {
    private static Dialog sLastShownProgressDialog;

    public static void dismissLastShownProgressDialog() {
        if (sLastShownProgressDialog != null && sLastShownProgressDialog.isShowing()) {
            try {
                sLastShownProgressDialog.dismiss();
                sLastShownProgressDialog = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void modifyProgressDialogText(Context context, String msg, int msgId) {
        if (sLastShownProgressDialog != null && sLastShownProgressDialog.isShowing()) {
            TextView messageText = (TextView) sLastShownProgressDialog.findViewById(R.id.progress_text);
            if (msg != null) {
                messageText.setText(msg);
            } else if (msgId > 0) {
                messageText.setText(msgId);
            }
        }
    }

    public static Dialog showProgressDialog(Context context, String msg) {
        return showProgressDialog(context, msg, 0);
    }

    public static Dialog showProgressDialog(Context context, int mesId) {
        return showProgressDialog(context, null, 0);
    }

    private static Dialog showProgressDialog(Context context, String message, int mesId) {
        dismissLastShownProgressDialog();
        Dialog dialog = null;
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_animation_progressbar,
                null);
        dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        TextView messageText = (TextView) view.findViewById(R.id.progress_text);
        if (message != null) {
            messageText.setText(message);
        } else if (mesId > 0) {
            messageText.setText(mesId);
        }
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        sLastShownProgressDialog = dialog;
        return dialog;
    }
    
    public static Dialog showTipsDialog(Activity activity, String message, int mesResId,
            final OnClickListener lisYes) {
        return showPopupDialog(activity, message, mesResId, true, lisYes, null);
    }

    public static Dialog showPopupDialog(Activity activity, String message, int mesResId,
            boolean onlyShowYesButton, final OnClickListener lisYes, final OnClickListener lisNo) {
        View view = activity.getLayoutInflater().inflate(R.layout.dialog_pop, null);
        final Dialog dialog = new Dialog(activity, R.style.dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        TextView messageView = (TextView) view.findViewById(R.id.messageView);
        ImageButton buttonOK = (ImageButton) view.findViewById(R.id.buttonOK);
        ImageButton buttonCancel = (ImageButton) view.findViewById(R.id.buttonCancel);
        buttonCancel.setVisibility(onlyShowYesButton ? View.GONE : View.VISIBLE);
        if (message != null) {
            messageView.setText(message);
        } else {
            messageView.setText(mesResId);
        }
        buttonOK.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (lisYes != null) {
                    lisYes.onClick(v);
                }
            }
        });
        buttonCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (lisNo != null) {
                    lisNo.onClick(v);
                }
            }
        });
        if (!activity.isFinishing()) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.show();
                }
            });
        }
        return dialog;
    }

    public static Dialog getPopupDialog(Activity activity, String message, int mesResId,
            int txtYes, int txtNo, final OnClickListener lisYes, final OnClickListener lisNo) {
        final Dialog dialog = new Dialog(activity, R.style.dialog);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.dialog_txt_yesno);
        TextView tvMessage = (TextView) dialog.findViewById(R.id.dialog_tv_message);
        Button btnYes = (Button) dialog.findViewById(R.id.dialog_btn_yes);
        Button btnNo = (Button) dialog.findViewById(R.id.dialog_btn_no);

        if (message != null) {
            tvMessage.setText(message);
        } else {
            tvMessage.setText(mesResId);
        }
        btnYes.setText(txtYes);
        btnNo.setText(txtNo);
        btnYes.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lisYes != null) {
                    lisYes.onClick(v);
                }
                dialog.dismiss();
            }
        });
        btnNo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lisNo != null) {
                    lisNo.onClick(v);
                }
                dialog.dismiss();
            }
        });
        return dialog;
    }
}
